﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTOFrameworks;
using System.Data.Common;
using FooFun.Faq.Dal;
using FooFun.DataElements.Dtos;

namespace PhysicalDataProvider
{
    public class ReplyIndexService : ITopicService, IReplyService
    {
        DEDataProvider dataProvider;

        public DEDataProvider DataProvider
        {
            get { return dataProvider; }
            set { dataProvider = value; }
        }

        public void InsertIndex(Guid topicId, Guid replyAccountId,
            DateTime editTime, Guid forumId
            )
        {
            DbParameter para1 = DataProvider.CreateParameter();
            para1.Value = DataProviderUtil.GetGuidBase64(topicId);

            DbParameter para2 = DataProvider.CreateParameter();
            para2.Value = DataProviderUtil.GetGuidBase64(replyAccountId);

            DbParameter para3 = DataProvider.CreateParameter();
            para3.Value = editTime.ToString();

            DbParameter para4 = DataProvider.CreateParameter();
            para4.Value = DataProviderUtil.GetGuidBase64(forumId);

            DataProvider.ExecuteNonQuery(@"DELETE FROM t_ReplyIndex
WHERE TopicId=? AND ReplyAccountId=?",
                para1, para2);

            DataProvider.ExecuteNonQuery(@"INSERT INTO t_ReplyIndex (TopicId,ReplyAccountId,EditTime,ForumId) VALUES (?,?,?,?)",
                para1, para2, para3, para4);

        }

        #region ITopicService 成员

        public void CreateTopic(string topicTitle, string remark, Guid eventId, Guid forumId, Guid userId)
        {
            return;
        }

        public void MoveTopic(IDataElement topic, Guid forumId)
        {
            DbParameter para1 = DataProvider.CreateParameter();
            para1.Value = DataProviderUtil.GetGuidBase64
                (topic["@TopicId"].Value);

            DbParameter para4 = DataProvider.CreateParameter();
            para4.Value = DataProviderUtil.GetGuidBase64(forumId);

            DataProvider.ExecuteNonQuery(@"UPDATE t_ReplyIndex 
SET ForumId=? WHERE TopicId = ?", para4, para1);

        }

        public void RemoveTopic(Guid topicId)
        {
            DbParameter para1 = DataProvider.CreateParameter();
            para1.Value = DataProviderUtil.GetGuidBase64(topicId);

            DataProvider.ExecuteNonQuery(@"DELETE FROM t_ReplyIndex
WHERE TopicId = ?", para1);
        }

        #endregion

        #region IReplyService 成员

        public void AddReply(IDataElement topic, Guid eventId, Guid userId, string reply)
        {
            InsertIndex(
            DataProviderUtil.GetGuid(topic["@TopicId"].Value),
            userId, DateTime.Now,
            DataProviderUtil.GetGuid(topic["@ForumId"].Value));
        }

        public void Conclude(IDataElement topic, Guid eventId, Guid replyId, string conclusion)
        {
            return;
        }

        #endregion
    }
}
